home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / GPARSE_TEST.PY < prev    next >
Encoding:
Python Source  |  1999-03-09  |  6.5 KB  |  191 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. "$Id: gparse_test.py,v 1.3 1999/03/10 00:15:08 klm Exp $"
  65. import sys, parser, symbol, token
  66. from symbol import *
  67. from token import *
  68. from parser import sequence2ast, compileast, ast2list
  69. from gparse import *
  70.  
  71. def pretty(s):
  72.     l=ast2list(parser.expr(s))
  73.     print l
  74.     pret(l)
  75.  
  76. def astl(s): return parser.ast2list(parser.expr(s))
  77.  
  78. def pret(ast, level=0):
  79.     if ISTERMINAL(ast[0]): print '  '*level, ast[1]
  80.     else:
  81.         print '  '*level, sym_name[ast[0]], '(%s)' % ast[0]
  82.         for a in ast[1:]:
  83.             pret(a,level+1)
  84.     
  85. def tpretty():
  86.     print 60*'='
  87.     for arg in sys.argv[1:]:
  88.       print
  89.       print arg
  90.       pretty(arg)
  91.       print 60*'='
  92.  
  93. def check(expr1=None, expr2=None):
  94.     ok=1
  95.     expr1=expr1 or sys.argv[1]
  96.     expr2=expr2 or sys.argv[2]
  97.     l1=munge(astl(expr1))
  98.     l2=astl(expr2)
  99.     try: c1=compileast(sequence2ast(l1))
  100.     except:
  101.         traceback.print_exc
  102.         c1=None
  103.     c2=compileast(sequence2ast(l2))
  104.     if c1 !=c2:
  105.         ok=0
  106.         print 'test failed', expr1, expr2
  107.         print
  108.         print l1
  109.         print
  110.         print l2
  111.         print
  112.  
  113.     ast=parser.sequence2ast(l1)
  114.     c=parser.compileast(ast)
  115.  
  116.     pretty(expr1)
  117.     pret(l1)
  118.     pret(l2)
  119.  
  120.     return ok
  121.     
  122. def spam():
  123.     # Regression test
  124.     import traceback
  125.     ok=1
  126.     for expr1, expr2 in (
  127.         ("a*b",         "__guarded_mul__(_vars, a, b)"),
  128.         ("a*b*c",
  129.          "__guarded_mul__(_vars, __guarded_mul__(_vars, a, b), c)"
  130.          ),
  131.         ("a.b",         "__guarded_getattr__(_vars, a, 'b')"),
  132.         ("a[b]",        "__guarded_getitem__(_vars, a, b)"),
  133.         ("a[b,c]",      "__guarded_getitem__(_vars, a, b, c)"),
  134.         ("a[b:c]",      "__guarded_getslice__(_vars, a, b, c)"),
  135.         ("a[:c]",       "__guarded_getslice__(_vars, a, 0, c)"),
  136.         ("a[b:]",       "__guarded_getslice__(_vars, a, b)"),
  137.         ("a[:]",        "__guarded_getslice__(_vars, a)"),
  138.         ("_vars['sequence-index'] % 2",
  139.          "__guarded_getitem__(_vars, _vars, 'sequence-index') % 2"
  140.          ),
  141.         ):
  142.         l1=munge(astl(expr1))
  143.         l2=astl(expr2)
  144.         try: c1=compileast(sequence2ast(l1))
  145.         except:
  146.             traceback.print_exc
  147.             c1=None
  148.         c2=compileast(sequence2ast(l2))
  149.         if c1 !=c2:
  150.             ok=0
  151.             print 'test failed', expr1, expr2
  152.             print
  153.             print l1
  154.             print
  155.             print l2
  156.             print
  157.  
  158.         ast=parser.sequence2ast(l1)
  159.         c=parser.compileast(ast)
  160.         
  161.     if ok: print 'all tests succeeded'
  162.  
  163. if __name__=='__main__':
  164.     try:
  165.         c=sys.argv[1]
  166.         del sys.argv[1]
  167.         globals()[c]()
  168.     except: spam()    
  169.  
  170.